package sza.oracle.apps.ap.oie.print.server;

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OAApplicationModuleImpl;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.XMLInterface;
import oracle.jbo.domain.BlobDomain;
import oracle.jbo.server.ViewLinkImpl;

import oracle.xml.parser.v2.XMLNode;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class PrintAMImpl extends OAApplicationModuleImpl {
    /**This is the default constructor (do not remove)
     */
    public PrintAMImpl() {
    }

    /**Sample main for debugging Business Components code using the tester.
     */
    public static void main(String[] args) { /* package name */
        /* Configuration Name */launchTester("sza.oracle.apps.ap.oie.print.server", 
                                             "PrintAMLocal");
    }


    /**Container's getter for PrintHeaderVO1
     */
    public PrintHeaderVOImpl getPrintHeaderVO1() {
        return (PrintHeaderVOImpl)findViewObject("PrintHeaderVO1");
    }


    public void getPrintRecord(String statusInput, String submitPersonId, 
                               String submitDateInput, String submitDateTo, 
                               String reportTypeInput, String firstApprId, 
                               String firstApprDateInput, 
                               String firstApprDateTo, 
                               String reportHeaderIdInput, String lastApprId, 
                               String lastApprDateInput, String lastApprDateTo, 
                               String costCenterInput, String paymethodInput,String shenpibegin, String shenpito) {
        PrintHeaderVOImpl vo = this.getPrintHeaderVO1();
        vo.query(statusInput, submitPersonId, submitDateInput, submitDateTo, 
                 reportTypeInput, firstApprId, firstApprDateInput, 
                 firstApprDateTo, reportHeaderIdInput, lastApprId, 
                 lastApprDateInput, lastApprDateTo, costCenterInput, 
                 paymethodInput,shenpibegin,shenpito);
                  
    }
    
    public void getSigleRecord(String reportHeaderId){
        PrintHeaderVOImpl vo = this.getPrintHeaderVO1();
        vo.query(reportHeaderId);
    }
    
  

    public String printPayment() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        OAViewObject vo = (OAViewObject)findViewObject("PrintHeaderVO1");
        if(vo.getFetchedRowCount()<=0)
        {
          throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
        }
        Row[] selectedRows = vo.getFilteredRowsInFetchedRange("Selected", "Y");
        if(selectedRows.length <=0 )
        {
          throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
        }
        Row row = null;
        String reportHeaderId = "";
        XMLNode node  = null;
        String recordTagStart = "<PrintHeaderVO>";
        String recordTagEnd = "</PrintHeaderVO>";
        String allTag = "<AllRecord>";
        String allTagEnd = "</AllRecord>";
        
        int length = selectedRows.length;
        try {
            outputStream.write(allTag.getBytes());
            for (int i = 0; i < length; i++) {
                outputStream.write(recordTagStart.getBytes());
                row = selectedRows[i];
                node = 
                    (XMLNode)row.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                reportHeaderId = row.getAttribute("ReportHeaderId") + "";
                
                PrintLineVOImpl printLineVO =  getPrintLineVO1();
                printLineVO.query(reportHeaderId);
                node = 
                    (XMLNode)printLineVO.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                
                PrintApprovalVOImpl approvalVO =  getPrintApprovalVO1();
                approvalVO.query(reportHeaderId);
                node = 
                    (XMLNode)approvalVO.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                
                outputStream.write(recordTagEnd.getBytes());
            }
            outputStream.write(allTagEnd.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return outputStream.toString();
    }
    
    public String printSiglePayment() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        OAViewObject vo = (OAViewObject)findViewObject("PrintHeaderVO1");
        
//        if(vo.getFetchedRowCount()<=0)
//        {
//          throw new OAException("getFetchedRowCount:"+vo.getFetchedRowCount()+"getRowCount(): "+vo.getRowCount());
//        }
//        Row[] selectedRows = vo.getFilteredRowsInFetchedRange("Selected", "N");
//        if(selectedRows.length <=0 )
//        {
//            throw new OAException("getFetchedRowCount:"+vo.getFetchedRowCount()+"getRowCount(): "+vo.getRowCount());
//        }
        Row[] selectedRows = vo.getFilteredRows("Selected", "N");
        Row row = null;
        String reportHeaderId = "";
        XMLNode node  = null;
        String recordTagStart = "<PrintHeaderVO>";
        String recordTagEnd = "</PrintHeaderVO>";
        String allTag = "<AllRecord>";
        String allTagEnd = "</AllRecord>";
        
        int length = selectedRows.length;
        try {
            outputStream.write(allTag.getBytes());
            for (int i = 0; i < length; i++) {
                outputStream.write(recordTagStart.getBytes());
                row = selectedRows[i];
                node = 
                    (XMLNode)row.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                reportHeaderId = row.getAttribute("ReportHeaderId") + "";
                
                PrintLineVOImpl printLineVO =  getPrintLineVO1();
                printLineVO.query(reportHeaderId);
                node = 
                    (XMLNode)printLineVO.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                
                PrintApprovalVOImpl approvalVO =  getPrintApprovalVO1();
                approvalVO.query(reportHeaderId);
                node = 
                    (XMLNode)approvalVO.writeXML(4, XMLInterface.XML_OPT_ASSOC_CONSISTENT);
                node.print(outputStream);
                
                outputStream.write(recordTagEnd.getBytes());
            }
            outputStream.write(allTagEnd.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return outputStream.toString();
    }


    /**Container's getter for ExpenseTemplateVO1
     */
    public ExpenseTemplateVOImpl getExpenseTemplateVO1() {
        return (ExpenseTemplateVOImpl)findViewObject("ExpenseTemplateVO1");
    }

    /**Container's getter for CostCenterVO1
     */
    public CostCenterVOImpl getCostCenterVO1() {
        return (CostCenterVOImpl)findViewObject("CostCenterVO1");
    }

    /**Container's getter for PaymentMethodVO1
     */
    public PaymentMethodVOImpl getPaymentMethodVO1() {
        return (PaymentMethodVOImpl)findViewObject("PaymentMethodVO1");
    }

    /**Container's getter for ReportStatusVO1
     */
    public ReportStatusVOImpl getReportStatusVO1() {
        return (ReportStatusVOImpl)findViewObject("ReportStatusVO1");
    }

    /**Container's getter for ApprovalSignatureVO1
     */
    public ApprovalSignatureVOImpl getApprovalSignatureVO1() {
        return (ApprovalSignatureVOImpl)findViewObject("ApprovalSignatureVO1");
    }

    public void createSignatureImage(String reportHeadIds, String path) {
        ApprovalSignatureVOImpl vo = this.getApprovalSignatureVO1();
        vo.query(reportHeadIds);
        BlobDomain image = null;
        String imageName = "";
        //        int fetchedRowCount = vo.getFetchedRowCount();
        int rowCount = vo.getRowCount();
        System.err.println("fetchrow :" + rowCount);
        Row row = null;
        RowSetIterator iterator = vo.createRowSetIterator("iterator");
        if (rowCount > 0) {
            iterator.setRangeStart(0);
            iterator.setRangeSize(rowCount);
            for (int i = 0; i < rowCount; i++) {
                image = null;
                row = iterator.getRowAtRangeIndex(i);
                image = (BlobDomain)row.getAttribute("FileData");
                imageName = (String)row.getAttribute("FileName");
                if (image != null) {
                    initImage(image, path, imageName);
                }
            }
        }


    }

    private void initImage(BlobDomain image, String phyPath, 
                           String imageName) {
        File directory = new File(phyPath);
        if (!directory.exists())
            directory.mkdirs();
        File imageFile = new File(directory, imageName);
        if (!imageFile.exists()) {
            try {
                fromInputToOutput(image.getBinaryStream(), 
                                  new FileOutputStream(imageFile));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            System.out.println(imageFile.getAbsolutePath());
        }

    }
    /*????????????byte?*/

    public void fromInputToOutput(InputStream inputstream, 
                                  OutputStream outputstream) throws IOException {
        byte abyte0[] = new byte[255];
        for (int i = 255; i == 255; ) {
            i = inputstream.read(abyte0);
            if (i < 0)
                break;
            outputstream.write(abyte0, 0, i);
        }
        outputstream.close();
        inputstream.close();
    }


    /**Container's getter for PrintLineVO1
     */
    public PrintLineVOImpl getPrintLineVO1() {
        return (PrintLineVOImpl)findViewObject("PrintLineVO1");
    }

    /**Container's getter for PrintApprovalVO1
     */
    public PrintApprovalVOImpl getPrintApprovalVO1() {
        return (PrintApprovalVOImpl)findViewObject("PrintApprovalVO1");
    }

    /**Container's getter for ExpTypeVO1
     */
    public ExpTypeVOImpl getExpTypeVO1() {
        return (ExpTypeVOImpl)findViewObject("ExpTypeVO1");
    }

    /**Container's getter for ExpenseReportHisVO1
     */
    public ExpenseReportHisVOImpl getExpenseReportHisVO1() {
        return (ExpenseReportHisVOImpl)findViewObject("ExpenseReportHisVO1");
    }
    public void getHisRecord(String employee_id,String INVOICE_NUM,String WEEK_END_DATE,String WEEK_END_DATE_To,String REPORT_SUBMITTED_DATE,String REPORT_SUBMITTED_DATE_To,String EXPENSE_STATUS_CODE,String entered_date,String entered_date_To) {
        ExpenseReportHisVOImpl vo = this.getExpenseReportHisVO1();
        vo.query(employee_id,INVOICE_NUM, WEEK_END_DATE,WEEK_END_DATE_To,REPORT_SUBMITTED_DATE,REPORT_SUBMITTED_DATE_To,EXPENSE_STATUS_CODE,entered_date,entered_date_To);
             
    }
}
